package demo1;

import java.util.Arrays;

//泛型类
class MyArray<T> {

    //泛型数组
    //public T[] array1 = new T[10];//泛型类中不能实例化一个泛型类型的数组
    public T[] array = (T[])new Object[10];

    public T getPos(int pos) {
        return array[pos];
    }

    public void setPos(int pos, T val) {
        array[pos] = val;
    }
}

class MyArray2<T> {
    //这样写比较好
    public Object[] array = new Object[10];

    public T getPos(int pos) {
        return (T)array[pos];
    }

    public void setPos(int pos, T val) {
        array[pos] = val;
    }
}


//泛型类
class Alg1<T extends Comparable<T>> {

    //泛型方法
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2{

    public  <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3{

    //静态泛型方法
    public static <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class TestDemo1 {
    public static void main(String[] args) {
        Integer[] array = {2,3,1,5,22,66,3};
        int ret = Alg3.findMax(array);
        System.out.println(ret);
    }

    public static void main4(String[] args) {
        Alg2 alg2 = new Alg2();
        Integer[] array = {2,3,1,5,22,66,3};
        int ret = alg2.<Integer>findMax(array);//方法名前面的参数可以省略
        System.out.println(ret);
    }

    public static void main3(String[] args) {
        Alg1<Integer> alg1 = new Alg1<>();
        Integer[] array = {2,3,1,5,22,66,3};
        int ret = alg1.findMax(array);
        System.out.println(ret);
    }

    public static void main2(String[] args) {
        MyArray2<Integer> array = new MyArray2<>();
        array.setPos(0, 10);
        int ret = array.getPos(0);
        System.out.println(ret);
    }

    public static void main1(String[] args) {
        //实例化对象的同时，指定当前泛型类的 指定参数是Interger
        MyArray<Integer> array = new MyArray<>();//new 里面的类型可以不写
        //就可以存储 指定的数据类型
        array.setPos(0, 10);
        int ret = array.getPos(0);
        System.out.println(ret);
    }
}
